/*
 * File: FCHC.c
 *
 * Code generated for Simulink model 'FCHC'.
 *
 * Model version                  : 1.1
 * Simulink Coder version         : 9.5 (R2021a) 14-Nov-2020
 * C/C++ source code generated on : Tue Jul  2 14:05:47 2024
 *
 * Target selection: ert.tlc
 * Embedded hardware selection: ARM Compatible->ARM Cortex-M
 * Code generation objective: MISRA C:2012 guidelines
 * Validation result: Not run
 */

#include "FCHC.h"
#include "FCHC_private.h"

/* Named constants for Chart: '<S4>/BGCharging for step detection' */
#define IN_BCP                         ((uint8_T)1U)
#define IN_BRO00                       ((uint8_T)2U)
#define IN_BROAA                       ((uint8_T)3U)
#define IN_BROSend                     ((uint8_T)4U)
#define IN_Charging_cfg                ((uint8_T)1U)
#define IN_Charging_charging           ((uint8_T)2U)
#define IN_Charging_default            ((uint8_T)3U)
#define IN_Charging_errorFrame         ((uint8_T)4U)
#define IN_Charging_finish             ((uint8_T)1U)
#define IN_Charging_handle             ((uint8_T)5U)
#define IN_Charging_over               ((uint8_T)6U)
#define IN_Charging_recog_CRM00        ((uint8_T)7U)
#define IN_Charging_recog_CRMAA        ((uint8_T)8U)
#define IN_Charging_stop               ((uint8_T)2U)
#define IN_NO_ACTIVE_CHILD             ((uint8_T)0U)
#define IN_NoBROSend                   ((uint8_T)5U)

/* Named constants for Chart: '<S7>/Chart' */
#define IN_CC2check                    ((uint8_T)1U)
#define IN_ResetMode                   ((uint8_T)2U)
#define IN_SetMode                     ((uint8_T)3U)
#define IN_TimeoutCheck                ((uint8_T)4U)

/* Block signals (default storage) */
B_FCHC_T FCHC_B;

/* Block states (default storage) */
DW_FCHC_T FCHC_DW;

/* Real-time model */
static RT_MODEL_FCHC_T FCHC_M_;
RT_MODEL_FCHC_T *const FCHC_M = &FCHC_M_;

/* Exported data definition */

/* Definition for custom storage class: ExportToFile */
uint16_T KcFCHC_CC2sustaintimeMs = 1000U;/* Referenced by: '<S4>/Constant5' */

/* CC2电压范围确认持续时间限制 */
uint16_T KcFCHC_CnntLowVoltMv = 2328U; /* Referenced by: '<S4>/Constant3' */

/* CC2电压范围确认电压限值计数低压 */
uint16_T KcFCHC_CnntUpVoltMv = 2663U;  /* Referenced by: '<S4>/Constant2' */

/* CC2电压范围确认电压限值计数高压 */
uint16_T KcFCHC_CutoffMaxCellVoltMv = 4160U;/* Referenced by: '<S4>/Constant7' */

/* 电芯截止电压 */
uint16_T KcFCHC_CutoffPackVoltV = 4000U;/* Referenced by: '<S4>/Constant8' */

/* Pack截止电压(Factor:0.100000,Offset:0) */
uint16_T KcFCHC_MaxEblChrgSOC = 100U;  /* Referenced by: '<S4>/Constant9' */

/* 最大允许充电SOC */
uint16_T KcFCHC_ShortToGNDLowVoltMv = 0U;/* Referenced by: '<S4>/Constant1' */

/* CC2电压范围确认电压限值短地低压 */
uint16_T KcFCHC_ShortToGNDUpVoltMv = 500U;/* Referenced by: '<S4>/Constant' */

/* CC2电压范围确认电压限值短地高压 */
uint16_T KcFCHC_UnCnntLowVoltMv = 5000U;/* Referenced by: '<S4>/Constant4' */

/* CC2电压范围确认电压限值不计数低压 */

/* Const memory section */
/* Definition for custom storage class: Const */
const uint16_T ScFCHC_BattRateCapAh = 232U;/* Referenced by: '<S11>/Constant1' */

/* 电池额定容量 */

/* Definition for custom storage class: ExportToFile */
boolean_T SbFCHC_BCLSendFlg;          /* '<S4>/BGCharging for step detection' */

/* BCL发送标志 */
boolean_T SbFCHC_BCPSendFlg;          /* '<S4>/BGCharging for step detection' */

/* BCP发送标志 */
boolean_T SbFCHC_BCSSendFlg;          /* '<S4>/BGCharging for step detection' */

/* BCS发送标志 */
boolean_T SbFCHC_BEMSendFlg;          /* '<S4>/BGCharging for step detection' */

/* BEM发送标志 */
boolean_T SbFCHC_BHMSendFlg;          /* '<S4>/BGCharging for step detection' */

/* BHM发送标志 */
boolean_T SbFCHC_BRMSendFlg;          /* '<S4>/BGCharging for step detection' */

/* BRM发送标志 */
boolean_T SbFCHC_BROSendFlg;          /* '<S4>/BGCharging for step detection' */

/* BRO发送标志 */
boolean_T SbFCHC_BSDSendFlg;          /* '<S4>/BGCharging for step detection' */

/* BSD发送标志 */
boolean_T SbFCHC_BSMSendFlg;          /* '<S4>/BGCharging for step detection' */

/* BSM发送标志 */
boolean_T SbFCHC_BSTSendFlg;          /* '<S4>/BGCharging for step detection' */

/* BST发送标志 */
boolean_T SbFCHC_CCSTimeOutFlg;       /* '<S4>/BGCharging for step detection' */

/* CCS接收超时标志 */
boolean_T SbFCHC_CEMTimeOutFlg;       /* '<S4>/BGCharging for step detection' */

/* CEM接收超时标志 */
boolean_T SbFCHC_CHMTimeoutFlg;       /* '<S4>/BGCharging for step detection' */

/* CHM超时 */
boolean_T SbFCHC_CMLTimeOutFlg;       /* '<S4>/BGCharging for step detection' */

/* CML超时标志 */
boolean_T SbFCHC_CRM00TimeOutFlg;     /* '<S4>/BGCharging for step detection' */

/* CRM00发送超时标志 */
boolean_T SbFCHC_CRMAATimeOutFlg;     /* '<S4>/BGCharging for step detection' */

/* CRMAA发送超时标志 */
boolean_T SbFCHC_CROTimeOutFlg;       /* '<S4>/BGCharging for step detection' */

/* CRO发送超时标志 */
boolean_T SbFCHC_CSDTimeOutFlg;       /* '<S4>/BGCharging for step detection' */

/* CSD接收超时标志 */
boolean_T SbFCHC_CSTTimeOutFlg;       /* '<S4>/BGCharging for step detection' */

/* CST报文超时标志 */
boolean_T SbFCHC_ChrgFinishOvrFlg;    /* '<S4>/BGCharging for step detection' */

/* 充电结束标志 */
boolean_T SbFCHC_ChrgFullFlg;          /* '<S4>/Subsystem' */

/* 满充标志 */
boolean_T SbFCHC_DCChrgModeFlg;        /* '<S7>/Chart' */

/* DC直流充电模式标志 */
boolean_T SbFCHC_ForbidChrgFlg;        /* '<S8>/Logical Operator1' */

/* 禁止充电标志 */
boolean_T SbFCHC_ForceDrawGunFlg;      /* '<S9>/Logical Operator' */

/* 强制拔枪标志 */
boolean_T SbFCHC_ParameMatchFlg;      /* '<S4>/BGCharging for step detection' */

/* 参数匹配标志 */
boolean_T SbFLTD_OtherFltFlg;          /* '<S8>/Logical Operator' */

/* 其他故障 */
uint8_T ScFCHC_BROValue;              /* '<S4>/BGCharging for step detection' */

/* BRO的值 */
uint16_T ScFCHC_CC2ConnectSts;         /* '<S6>/Data Type Conversion3' */

/* CC2连接状态；1）正常连接；2）短地；3）异常连接 */
uint16_T ScFCHC_ChrgRemainTime;        /* '<S4>/充电剩余时间计算' */

/* 充电剩余时间min */
uint16_T ScFCHC_ChrgState;            /* '<S4>/BGCharging for step detection' */

/* 充电状态；<0>默认；<1>握手；<2>辨识00；<3>辨识AA；<4>BRO；<5>充电中；<6>结束；<7>统计数据；<8>错误报文； */
boolean_T ScFCHC_DCChrgRelayCtrlFlg;  /* '<S4>/BGCharging for step detection' */

/* 直流充电继电器控制标志 */
uint8_T ScFCHC_GBEditionFlg;          /* '<S4>/BGCharging for step detection' */

/* 国标版本 */
uint16_T ScFCHC_VhlChrgMode;          /* '<S4>/BGCharging for step detection' */

/* 车辆充电模式 */

/* Forward declaration for local functions */
static void Charging_cfg(void);
static void Charging_over(void);

/*
 * Output and update for atomic system:
 *    '<S13>/FCHC_IncreaseTimer'
 *    '<S14>/FCHC_IncreaseTimer'
 *    '<S15>/FCHC_IncreaseTimer'
 */
void FCHC_IncreaseTimer(boolean_T Reset, boolean_T Enable, uint16_T UpperLim, real_T
                   Step, B_IncreaseTimer_FCHC_T *localB, DW_IncreaseTimer_FCHC_T
                   *localDW)
{
    real_T Switch4;
    uint16_T Switch3;
    if (Reset)
    {
        Switch3 = 0U;
    }
    else
    {
        if (Enable)
        {
            Switch4 = Step + (real_T)localDW->U_DSTATE;
        }
        else
        {
            Switch4 = localDW->U_DSTATE;
        }

        if (Switch4 < 65536.0)
        {
            if (Switch4 >= 0.0)
            {
                Switch3 = (uint16_T)Switch4;
            }
            else
            {
                Switch3 = 0U;
            }
        }
        else
        {
            Switch3 = MAX_uint16_T;
        }
    }

    if (Switch3 >= UpperLim)
    {
        Switch3 = UpperLim;
    }

    localB->RelationalOperator_cv = (Switch3 == UpperLim);
    localDW->U_DSTATE = Switch3;
}

/* Output and update for atomic system: '<S20>/FCHC_IncreaseTimer' */
void FCHC_IncreaseTimer_b(boolean_T Reset, boolean_T Enable, real_T UpperLim, real_T
                     Step, B_IncreaseTimer_FCHC_n_T *localB,
                     DW_IncreaseTimer_FCHC_i_T *localDW)
{
    real_T Switch3;
    if (Reset)
    {
        Switch3 = 0.0;
    }
    else if (Enable)
    {
        Switch3 = Step + localDW->U_DSTATE;
    }
    else
    {
        Switch3 = localDW->U_DSTATE;
    }

    if (Switch3 >= UpperLim)
    {
        Switch3 = UpperLim;
    }

    localB->RelationalOperator_b = (Switch3 == UpperLim);
    localDW->U_DSTATE = Switch3;
}

/* Function for Chart: '<S4>/BGCharging for step detection' */
static void Charging_cfg(void)
{
    uint32_T qY;
    ScFCHC_ChrgState = 4U;
    if (SbFCHC_CROIDFlg && (ScCCAN_CROValue == 170) && (ScFCHC_BROValue == 170))
    {
        FCHC_DW.is_Charging_cfg = IN_NO_ACTIVE_CHILD;
        SbFCHC_BROSendFlg = false;
        SbFCHC_BCPSendFlg = false;
        FCHC_DW.is_c4_FCHC = IN_Charging_charging;
        ScFCHC_ChrgState = 5U;
        SbFCHC_BCLSendFlg = true;
        SbFCHC_BCSSendFlg = true;
        SbFCHC_BSMSendFlg = false;
        FCHC_DW.CCSTimeOutFlgCnt = 1000U;
    }
    else if (SbFCHC_CMLTimeOutFlg || SbFCHC_CROTimeOutFlg ||
             ((FCHC_DW.BROValueCnt == 0) && (ScFCHC_BROValue == 0)))
    {
        FCHC_DW.is_Charging_cfg = IN_NO_ACTIVE_CHILD;
        SbFCHC_BROSendFlg = false;
        SbFCHC_BCPSendFlg = false;
        FCHC_DW.is_c4_FCHC = IN_Charging_errorFrame;
        ScFCHC_ChrgState = 8U;
        SbFCHC_BEMSendFlg = true;
        FCHC_DW.BEMSendFlgCnt = 30000U;
        SbFCHC_CEMTimeOutFlg = false;
        ScFCHC_VhlChrgMode = 0U;
    }
    else
    {
        switch (FCHC_DW.is_Charging_cfg)
        {
          case IN_BCP:
            if (SbCCAN_CMLIDFlg)
            {
                FCHC_DW.is_Charging_cfg = IN_BRO00;
                SbFCHC_BCPSendFlg = false;
                SbFCHC_BROSendFlg = true;
                ScFCHC_BROValue = 0U;
                FCHC_DW.BROValueCnt = 60000U;
            }
            else
            {
                if (SbFCHC_BCPSendFlg && (!SbCCAN_CMLIDFlg))
                {
                    qY = FCHC_DW.CMLTimeOutFlgCnt - 10U;
                    if (FCHC_DW.CMLTimeOutFlgCnt - 10U >
                            FCHC_DW.CMLTimeOutFlgCnt)
                    {
                        qY = 0U;
                    }

                    FCHC_DW.CMLTimeOutFlgCnt = (uint16_T)qY;
                }

                SbFCHC_CMLTimeOutFlg = (FCHC_DW.CMLTimeOutFlgCnt == 0);
            }
            break;

          case IN_BRO00:
            if (SbFCHC_ParameMatchFlg && ScACAN_VCUControl &&
                    SbCVMD_CellVoltAllGetFlg && (!SbFCHC_ForbidChrgFlg) &&
                    (!SbLVPD_KeyOnValidFlg) && (!SbSCHG_ACChrgModeFlg) &&
                    SbCTMD_SummaryTempVldFlg)
            {
                FCHC_DW.is_Charging_cfg = IN_BROAA;
                ScFCHC_BROValue = 170U;
                FCHC_DW.BROValueCnt = 5000U;
            }
            else
            {
                if ((ScHVMD_PackVoltV < ScCCAN_ChargerMinOutputVoltV) ||
                        (ScHVMD_PackVoltV > ScCCAN_ChargerMaxOutputVoltV))
                {
                    SbFCHC_ParameMatchFlg = false;
                }
                else
                {
                    SbFCHC_ParameMatchFlg = true;
                    ScFCHC_DCChrgRelayCtrlFlg = true;
                    ScFCHC_VhlChrgMode = 4U;
                }

                qY = FCHC_DW.BROValueCnt - 10U;
                if (FCHC_DW.BROValueCnt - 10U > FCHC_DW.BROValueCnt)
                {
                    qY = 0U;
                }

                FCHC_DW.BROValueCnt = (uint16_T)qY;
            }
            break;

          case IN_BROAA:
            if (!SbFCHC_CROIDFlg)
            {
                FCHC_DW.is_Charging_cfg = IN_BROSend;
                FCHC_DW.CROTimeOutFlgCnt = 5000U;
            }
            else if (SbFCHC_CROIDFlg && (ScCCAN_CROValue == 0))
            {
                FCHC_DW.is_Charging_cfg = IN_NoBROSend;
                FCHC_DW.CROTimeOutFlgCnt = 60000U;
            }
            else
            {
                qY = FCHC_DW.BROValueCnt - 10U;
                if (FCHC_DW.BROValueCnt - 10U > FCHC_DW.BROValueCnt)
                {
                    qY = 0U;
                }

                FCHC_DW.BROValueCnt = (uint16_T)qY;
            }
            break;

          case IN_BROSend:
            if (SbFCHC_CROIDFlg && (ScCCAN_CROValue == 0))
            {
                FCHC_DW.is_Charging_cfg = IN_NoBROSend;
                FCHC_DW.CROTimeOutFlgCnt = 60000U;
            }
            else
            {
                qY = FCHC_DW.CROTimeOutFlgCnt - 10U;
                if (FCHC_DW.CROTimeOutFlgCnt - 10U > FCHC_DW.CROTimeOutFlgCnt)
                {
                    qY = 0U;
                }

                FCHC_DW.CROTimeOutFlgCnt = (uint16_T)qY;
                SbFCHC_CROTimeOutFlg = ((FCHC_DW.CROTimeOutFlgCnt == 0) ||
                                        SbFCHC_CROTimeOutFlg);
            }
            break;

          default:
            /* case IN_NoBROSend: */
            if (!SbFCHC_CROIDFlg)
            {
                FCHC_DW.is_Charging_cfg = IN_BROSend;
                FCHC_DW.CROTimeOutFlgCnt = 5000U;
            }
            else
            {
                qY = FCHC_DW.CROTimeOutFlgCnt - 10U;
                if (FCHC_DW.CROTimeOutFlgCnt - 10U > FCHC_DW.CROTimeOutFlgCnt)
                {
                    qY = 0U;
                }

                FCHC_DW.CROTimeOutFlgCnt = (uint16_T)qY;
                SbFCHC_CROTimeOutFlg = ((FCHC_DW.CROTimeOutFlgCnt == 0) ||
                                        SbFCHC_CROTimeOutFlg);
            }
            break;
        }
    }
}

/* Function for Chart: '<S4>/BGCharging for step detection' */
static void Charging_over(void)
{
    uint32_T qY;
    if (SbFCHC_CSTTimeOutFlg || SbFCHC_CSDTimeOutFlg)
    {
        switch (FCHC_DW.is_Charging_over)
        {
          case IN_Charging_finish:
            SbFCHC_BSDSendFlg = false;
            ScFCHC_DCChrgRelayCtrlFlg = false;
            FCHC_DW.is_Charging_over = IN_NO_ACTIVE_CHILD;
            break;

          case IN_Charging_stop:
            SbFCHC_BSTSendFlg = false;
            FCHC_DW.is_Charging_over = IN_NO_ACTIVE_CHILD;
            break;

          default:
            FCHC_DW.is_Charging_over = IN_NO_ACTIVE_CHILD;
            break;
        }

        FCHC_DW.is_c4_FCHC = IN_Charging_errorFrame;
        ScFCHC_ChrgState = 8U;
        SbFCHC_BEMSendFlg = true;
        FCHC_DW.BEMSendFlgCnt = 30000U;
        SbFCHC_CEMTimeOutFlg = false;
        ScFCHC_VhlChrgMode = 0U;
    }
    else
    {
        if (!SbFCHC_CSDIDFlg)
        {
            qY = FCHC_DW.CSDTimeOutFlgCnt - 10U;
            if (FCHC_DW.CSDTimeOutFlgCnt - 10U > FCHC_DW.CSDTimeOutFlgCnt)
            {
                qY = 0U;
            }

            FCHC_DW.CSDTimeOutFlgCnt = (uint16_T)qY;
        }

        SbFCHC_CSDTimeOutFlg = (FCHC_DW.CSDTimeOutFlgCnt == 0);
        if (FCHC_DW.is_Charging_over == IN_Charging_finish)
        {
            ScFCHC_ChrgState = 7U;
            if ((ScFCHC_CC2ConnectSts != 1) && (FCHC_DW.temporalCounter_i1 >=
                    300))
            {
                SbFCHC_BSDSendFlg = false;
                ScFCHC_DCChrgRelayCtrlFlg = false;
                FCHC_DW.is_Charging_over = IN_NO_ACTIVE_CHILD;
                FCHC_DW.is_c4_FCHC = IN_Charging_default;
                ScFCHC_ChrgState = 0U;
                ScFCHC_VhlChrgMode = 0U;
            }
            else if (ScBCUR_PackCurrA < 50)
            {
                ScFCHC_DCChrgRelayCtrlFlg = false;
            }
            else
            {
                qY = FCHC_DW.DCChrgRelayCtrlFlgCnt - 10U;
                if (FCHC_DW.DCChrgRelayCtrlFlgCnt - 10U >
                        FCHC_DW.DCChrgRelayCtrlFlgCnt)
                {
                    qY = 0U;
                }

                FCHC_DW.DCChrgRelayCtrlFlgCnt = (uint16_T)qY;
                ScFCHC_DCChrgRelayCtrlFlg = ((FCHC_DW.DCChrgRelayCtrlFlgCnt != 0)
                    && ScFCHC_DCChrgRelayCtrlFlg);
            }
        }
        else
        {
            /* case IN_Charging_stop: */
            ScFCHC_ChrgState = 6U;
            if (SbFCHC_BSTSendFlg && SbCCAN_CSTIDFlg &&
                    (FCHC_DW.temporalCounter_i1 >= 100))
            {
                SbFCHC_BSTSendFlg = false;
                FCHC_DW.is_Charging_over = IN_Charging_finish;
                FCHC_DW.temporalCounter_i1 = 0U;
                ScFCHC_ChrgState = 7U;
                SbFCHC_BSDSendFlg = true;
                FCHC_DW.DCChrgRelayCtrlFlgCnt = 10000U;
            }
            else
            {
                if (!SbCCAN_CSTIDFlg)
                {
                    qY = FCHC_DW.CSTTimeOutFlgCnt - 10U;
                    if (FCHC_DW.CSTTimeOutFlgCnt - 10U >
                            FCHC_DW.CSTTimeOutFlgCnt)
                    {
                        qY = 0U;
                    }

                    FCHC_DW.CSTTimeOutFlgCnt = (uint16_T)qY;
                }

                SbFCHC_CSTTimeOutFlg = (FCHC_DW.CSTTimeOutFlgCnt == 0);
            }
        }
    }
}

/* Model step function */
void FCHC_step(void)
{
    real_T tmp;
    uint32_T qY;
    boolean_T LogicalOperator1;
    boolean_T guard1 = false;
    LogicalOperator1 = ((ScINPT_CC2VoltV <= KcFCHC_ShortToGNDUpVoltMv) &&
                        (ScINPT_CC2VoltV >= KcFCHC_ShortToGNDLowVoltMv));
    FCHC_IncreaseTimer(!LogicalOperator1, LogicalOperator1, KcFCHC_CC2sustaintimeMs,
                  10.0, &FCHC_B.IncreaseTimer_o, &FCHC_DW.IncreaseTimer_o);
    LogicalOperator1 = ((ScINPT_CC2VoltV <= KcFCHC_CnntUpVoltMv) &&
                        (ScINPT_CC2VoltV >= KcFCHC_CnntLowVoltMv));
    FCHC_IncreaseTimer(!LogicalOperator1, LogicalOperator1, KcFCHC_CC2sustaintimeMs,
                  10.0, &FCHC_B.IncreaseTimer_d, &FCHC_DW.IncreaseTimer_d);
    LogicalOperator1 = ((ScINPT_CC2VoltV >= KcFCHC_CnntUpVoltMv) &&
                        (ScINPT_CC2VoltV <= KcFCHC_UnCnntLowVoltMv));
    FCHC_IncreaseTimer(!LogicalOperator1, LogicalOperator1, KcFCHC_CC2sustaintimeMs,
                  10.0, &FCHC_B.IncreaseTimer_ol, &FCHC_DW.IncreaseTimer_ol);
    if (FCHC_B.IncreaseTimer_d.RelationalOperator_cv)
    {
        FCHC_B.aCC2ConnectStsOpt = 1U;
    }
    else if (FCHC_B.IncreaseTimer_o.RelationalOperator_cv)
    {
        FCHC_B.aCC2ConnectStsOpt = 2U;
    }
    else if (FCHC_B.IncreaseTimer_ol.RelationalOperator_cv)
    {
        FCHC_B.aCC2ConnectStsOpt = 3U;
    }

    ScFCHC_CC2ConnectSts = FCHC_B.aCC2ConnectStsOpt;
    LogicalOperator1 = !ScACAN_VCUControl;
    SbFLTD_OtherFltFlg = (SbFLTD_ChrgCommFltFlg || LogicalOperator1);
    SbFCHC_ForbidChrgFlg = (SbFLTD_ChrgDisbldFlg ||
                            ScFLTD_ChrgGunBaseConctFltFlg ||
                            ScFLTD_ChrgGunBaseTmpHighFlg ||
                            ScFLTD_DCChrgRelayFltSts || SbFLTD_OtherFltFlg);
    tmp = (real_T)ScBCUR_PackCurrA * 0.1;
    if ((1 >= ScFCHC_CC2ConnectSts) && (0.0 > tmp))
    {
        FCHC_B.LogicalOperator_o = ((ScCVMD_MaxCellVoltMv >=
            KcFCHC_CutoffMaxCellVoltMv) || (ScHVMD_PackVoltV >=
            KcFCHC_CutoffPackVoltV) || (ScSOCE_PackDispSOC >=
            KcFCHC_MaxEblChrgSOC));
    }

    SbFCHC_ChrgFullFlg = FCHC_B.LogicalOperator_o;
    if (FCHC_DW.temporalCounter_i1 < 511U)
    {
        FCHC_DW.temporalCounter_i1++;
    }

    if (FCHC_DW.is_active_c4_FCHC == 0U)
    {
        FCHC_DW.is_active_c4_FCHC = 1U;
        FCHC_DW.is_c4_FCHC = IN_Charging_default;
        ScFCHC_ChrgState = 0U;
        ScFCHC_VhlChrgMode = 0U;
    }
    else
    {
        guard1 = false;
        switch (FCHC_DW.is_c4_FCHC)
        {
          case IN_Charging_cfg:
            Charging_cfg();
            break;

          case IN_Charging_charging:
            ScFCHC_ChrgState = 5U;
            if ((ScFCHC_CC2ConnectSts != 1) || LogicalOperator1 ||
                    SbLVPD_KeyOnValidFlg || SbSCHG_ACChrgModeFlg ||
                    SbFLTD_OtherFltFlg || SbFCHC_ChrgFullFlg || SbCCAN_CSTIDFlg)
            {
                SbFCHC_BCLSendFlg = false;
                SbFCHC_BCSSendFlg = false;
                SbFCHC_BSMSendFlg = false;
                FCHC_DW.is_c4_FCHC = IN_Charging_over;
                ScFCHC_VhlChrgMode = 0U;
                FCHC_DW.CSDTimeOutFlgCnt = 10000U;
                FCHC_DW.DCChrgRelayCtrlFlgCnt = 10000U;
                SbFCHC_CSDTimeOutFlg = false;
                FCHC_DW.is_Charging_over = IN_Charging_stop;
                FCHC_DW.temporalCounter_i1 = 0U;
                ScFCHC_ChrgState = 6U;
                SbFCHC_BSTSendFlg = true;
                FCHC_DW.CSTTimeOutFlgCnt = 5000U;
                SbFCHC_CSTTimeOutFlg = false;
            }
            else if (SbFCHC_CCSTimeOutFlg)
            {
                SbFCHC_BCLSendFlg = false;
                SbFCHC_BCSSendFlg = false;
                SbFCHC_BSMSendFlg = false;
                FCHC_DW.is_c4_FCHC = IN_Charging_errorFrame;
                ScFCHC_ChrgState = 8U;
                SbFCHC_BEMSendFlg = true;
                FCHC_DW.BEMSendFlgCnt = 30000U;
                SbFCHC_CEMTimeOutFlg = false;
                ScFCHC_VhlChrgMode = 0U;
            }
            else
            {
                SbFCHC_BSMSendFlg = (SbCCAN_CCSIDFlg || SbFCHC_BSMSendFlg);
                if (SbFCHC_BCLSendFlg && (!SbCCAN_CCSIDFlg))
                {
                    qY = FCHC_DW.CCSTimeOutFlgCnt - 10U;
                    if (FCHC_DW.CCSTimeOutFlgCnt - 10U >
                            FCHC_DW.CCSTimeOutFlgCnt)
                    {
                        qY = 0U;
                    }

                    FCHC_DW.CCSTimeOutFlgCnt = (uint16_T)qY;
                }

                SbFCHC_CCSTimeOutFlg = (FCHC_DW.CCSTimeOutFlgCnt == 0);
            }
            break;

          case IN_Charging_default:
            ScFCHC_ChrgState = 0U;
            if ((ScFCHC_CC2ConnectSts == 1) && SbFCHC_FastChrgWakeUpFlg)
            {
                FCHC_DW.is_c4_FCHC = IN_Charging_handle;
                ScFCHC_ChrgState = 1U;
                SbFCHC_ChrgFinishOvrFlg = false;
                ScFCHC_GBEditionFlg = 1U;
                FCHC_DW.CHMIDFlgCnt = 5000U;
                SbFCHC_CHMTimeoutFlg = false;
            }
            else if (SbFCHC_CRMIDFlg && (ScFCHC_CC2ConnectSts == 1))
            {
                ScFCHC_GBEditionFlg = 2U;

                /* 这一步为执行老国标流程，待确认。 */
                FCHC_DW.is_c4_FCHC = IN_Charging_recog_CRM00;
                ScFCHC_ChrgState = 2U;
                SbFCHC_BHMSendFlg = SbFCHC_CHMIDFlg;
                FCHC_DW.CRMIDFlgCnt = 30000U;
                SbFCHC_CRM00TimeOutFlg = false;
            }
            break;

          case IN_Charging_errorFrame:
            ScFCHC_ChrgState = 8U;
            if (SbFCHC_CRMIDFlg)
            {
                SbFCHC_BEMSendFlg = false;
                SbFCHC_ChrgFinishOvrFlg = true;
                ScFCHC_DCChrgRelayCtrlFlg = false;
                SbFCHC_CEMTimeOutFlg = false;
                FCHC_DW.is_c4_FCHC = IN_Charging_recog_CRM00;
                ScFCHC_ChrgState = 2U;
                SbFCHC_BHMSendFlg = SbFCHC_CHMIDFlg;
                FCHC_DW.CRMIDFlgCnt = 30000U;
                SbFCHC_CRM00TimeOutFlg = false;
            }
            else if (ScFCHC_CC2ConnectSts != 1)
            {
                SbFCHC_BEMSendFlg = false;
                SbFCHC_ChrgFinishOvrFlg = true;
                ScFCHC_DCChrgRelayCtrlFlg = false;
                SbFCHC_CEMTimeOutFlg = false;
                FCHC_DW.is_c4_FCHC = IN_Charging_default;
                ScFCHC_ChrgState = 0U;
                ScFCHC_VhlChrgMode = 0U;
            }
            else
            {
                ScFCHC_DCChrgRelayCtrlFlg = ((ScBCUR_PackCurrA >= 50) &&
                    ScFCHC_DCChrgRelayCtrlFlg);
                if (SbFCHC_BEMSendFlg && (!SbCCAN_CEMIDFlg))
                {
                    qY = FCHC_DW.BEMSendFlgCnt - 10U;
                    if (FCHC_DW.BEMSendFlgCnt - 10U > FCHC_DW.BEMSendFlgCnt)
                    {
                        qY = 0U;
                    }

                    FCHC_DW.BEMSendFlgCnt = (uint16_T)qY;
                }

                SbFCHC_CEMTimeOutFlg = (FCHC_DW.BEMSendFlgCnt == 0);
            }
            break;

          case IN_Charging_handle:
            ScFCHC_ChrgState = 1U;
            if ((ScFCHC_CC2ConnectSts != 1) || (!SbFCHC_FastChrgWakeUpFlg))
            {
                FCHC_DW.is_c4_FCHC = IN_Charging_default;
                ScFCHC_ChrgState = 0U;
                ScFCHC_VhlChrgMode = 0U;
            }
            else if (SbFCHC_CHMIDFlg)
            {
                guard1 = true;
            }
            else if (SbFCHC_CHMTimeoutFlg)
            {
                ScFCHC_GBEditionFlg = 2U;
                guard1 = true;
            }
            else
            {
                if (!SbFCHC_CHMIDFlg)
                {
                    qY = FCHC_DW.CHMIDFlgCnt - 10U;
                    if (FCHC_DW.CHMIDFlgCnt - 10U > FCHC_DW.CHMIDFlgCnt)
                    {
                        qY = 0U;
                    }

                    FCHC_DW.CHMIDFlgCnt = (uint16_T)qY;
                }
                else
                {
                    SbFCHC_BHMSendFlg = true;
                }

                SbFCHC_CHMTimeoutFlg = (FCHC_DW.CHMIDFlgCnt == 0);
            }
            break;

          case IN_Charging_over:
            Charging_over();
            break;

          case IN_Charging_recog_CRM00:
            ScFCHC_ChrgState = 2U;
            if (SbFCHC_CRMIDFlg && (ScCCAN_CRMValue == 0))
            {
                SbFCHC_BHMSendFlg = false;
                FCHC_DW.is_c4_FCHC = IN_Charging_recog_CRMAA;
                ScFCHC_ChrgState = 3U;
                SbFCHC_BRMSendFlg = true;
                FCHC_DW.CRMIDFlgCnt = 5000U;
                SbFCHC_CRMAATimeOutFlg = false;
            }
            else if (SbFCHC_CRM00TimeOutFlg)
            {
                SbFCHC_BHMSendFlg = false;
                FCHC_DW.is_c4_FCHC = IN_Charging_errorFrame;
                ScFCHC_ChrgState = 8U;
                SbFCHC_BEMSendFlg = true;
                FCHC_DW.BEMSendFlgCnt = 30000U;
                SbFCHC_CEMTimeOutFlg = false;
                ScFCHC_VhlChrgMode = 0U;
            }
            else
            {
                if ((ScCCAN_CRMValue != 0) || (!SbFCHC_CRMIDFlg))
                {
                    qY = FCHC_DW.CRMIDFlgCnt - 10U;
                    if (FCHC_DW.CRMIDFlgCnt - 10U > FCHC_DW.CRMIDFlgCnt)
                    {
                        qY = 0U;
                    }

                    FCHC_DW.CRMIDFlgCnt = (uint16_T)qY;
                }

                SbFCHC_CRM00TimeOutFlg = (FCHC_DW.CRMIDFlgCnt == 0);
            }
            break;

          default:
            /* case IN_Charging_recog_CRMAA: */
            ScFCHC_ChrgState = 3U;
            if ((!SbFCHC_CRMAATimeOutFlg) && (ScCCAN_CRMValue == 170))
            {
                SbFCHC_BRMSendFlg = false;
                FCHC_DW.is_c4_FCHC = IN_Charging_cfg;
                ScFCHC_ChrgState = 4U;
                SbFCHC_BCPSendFlg = true;
                FCHC_DW.CMLTimeOutFlgCnt = 5000U;
                SbFCHC_CMLTimeOutFlg = false;
                SbFCHC_CROTimeOutFlg = false;
                FCHC_DW.BROValueCnt = 60000U;
                FCHC_DW.is_Charging_cfg = IN_BCP;
            }
            else if (SbFCHC_CRMAATimeOutFlg)
            {
                SbFCHC_BRMSendFlg = false;
                FCHC_DW.is_c4_FCHC = IN_Charging_errorFrame;
                ScFCHC_ChrgState = 8U;
                SbFCHC_BEMSendFlg = true;
                FCHC_DW.BEMSendFlgCnt = 30000U;
                SbFCHC_CEMTimeOutFlg = false;
                ScFCHC_VhlChrgMode = 0U;
            }
            else
            {
                if (ScCCAN_CRMValue != 170)
                {
                    qY = FCHC_DW.CRMIDFlgCnt - 10U;
                    if (FCHC_DW.CRMIDFlgCnt - 10U > FCHC_DW.CRMIDFlgCnt)
                    {
                        qY = 0U;
                    }

                    FCHC_DW.CRMIDFlgCnt = (uint16_T)qY;
                }

                SbFCHC_CRMAATimeOutFlg = (FCHC_DW.CRMIDFlgCnt == 0);
            }
            break;
        }

        if (guard1)
        {
            FCHC_DW.is_c4_FCHC = IN_Charging_recog_CRM00;
            ScFCHC_ChrgState = 2U;
            SbFCHC_BHMSendFlg = SbFCHC_CHMIDFlg;
            FCHC_DW.CRMIDFlgCnt = 30000U;
            SbFCHC_CRM00TimeOutFlg = false;
        }
    }

    if (FCHC_DW.is_active_c1_FCHC == 0U)
    {
        FCHC_DW.is_active_c1_FCHC = 1U;
        if ((ScFCHC_CC2ConnectSts != 1) && ((ScFCHC_ChrgState == 0) ||
                (ScFCHC_ChrgState == 8)))
        {
            FCHC_DW.is_c1_FCHC = IN_ResetMode;
            FCHC_B.DCChrgModeFlg = false;
        }
        else if ((ScFCHC_CC2ConnectSts == 1) && (SbFCHC_CHMIDFlg ||
                  SbFCHC_CRMIDFlg))
        {
            FCHC_DW.is_c1_FCHC = IN_SetMode;
            FCHC_B.DCChrgModeFlg = true;
        }
        else
        {
            FCHC_DW.is_c1_FCHC = IN_CC2check;
            FCHC_DW.CC2conctFlg = ((ScFCHC_CC2ConnectSts == 1) &&
                                   SbFCHC_FastChrgWakeUpFlg);
        }
    }
    else
    {
        switch (FCHC_DW.is_c1_FCHC)
        {
          case IN_CC2check:
            if (FCHC_DW.CC2conctFlg)
            {
                FCHC_DW.is_c1_FCHC = IN_TimeoutCheck;
            }
            else
            {
                FCHC_DW.CC2conctFlg = ((ScFCHC_CC2ConnectSts == 1) &&
                                       SbFCHC_FastChrgWakeUpFlg);
            }
            break;

          case IN_ResetMode:
            FCHC_B.DCChrgModeFlg = false;
            break;

          case IN_SetMode:
            FCHC_B.DCChrgModeFlg = true;
            break;

          default:
            /* case IN_TimeoutCheck: */
            if (FCHC_DW.DCTimeoutFlg)
            {
                FCHC_DW.is_c1_FCHC = IN_SetMode;
                FCHC_B.DCChrgModeFlg = true;
            }
            else
            {
                FCHC_IncreaseTimer_b(!FCHC_DW.CC2conctFlg, FCHC_DW.CC2conctFlg, 100.0,
                                10.0, &FCHC_B.IncreaseTimer_br,
                                &FCHC_DW.IncreaseTimer_br);
                FCHC_DW.DCTimeoutFlg =
                    FCHC_B.IncreaseTimer_br.RelationalOperator_b;
            }
            break;
        }
    }

    SbFCHC_DCChrgModeFlg = FCHC_B.DCChrgModeFlg;
    if (4 == ScFCHC_ChrgState)
    {
        tmp = fmod(floor((1000.0 - (real_T)ScSOCE_PackDispSOC) * (real_T)
                         ScFCHC_BattRateCapAh * 60.0 / tmp), 65536.0);
        FCHC_B.DataTypeConversion = (uint16_T)(tmp < 0.0 ? (int32_T)(uint16_T)
            -(int16_T)(uint16_T)-tmp : (int32_T)(uint16_T)tmp);
    }

    ScFCHC_ChrgRemainTime = FCHC_B.DataTypeConversion;
    SbFCHC_ForceDrawGunFlg = ((ScFCHC_CC2ConnectSts != 1) &&
        (!SbFCHC_FastChrgWakeUpFlg));
}

/* Model initialize function */
void FCHC_initialize(void)
{
    /* Registration code */

    /* block I/O */

    /* custom signals */
    ScFCHC_CC2ConnectSts = ((uint16_T)1U);
    ScFCHC_ChrgRemainTime = ((uint16_T)1U);
    ScFCHC_VhlChrgMode = ((uint16_T)4U);
    ScFCHC_GBEditionFlg = ((uint8_T)1U);
    ScFCHC_BROValue = ((uint8_T)255U);
    ScFCHC_GBEditionFlg = 1U;
    ScFCHC_BROValue = MAX_uint8_T;
    ScFCHC_VhlChrgMode = 4U;
}

/*
 * File trailer for generated code.
 *
 * [EOF]
 */
